package com.web.base.wechat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blue.jdbc.JdbcDataSource;
import com.blue.jdbc.Page;
import com.web.base.wechat.exception.WechatTemplateException;
import com.web.base.wechat.dao.WechatTemplateDao;
import com.web.base.wechat.model.WechatTemplate;
import com.web.core.util.Const;

@Service
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class WechatTemplateService
{
	@Autowired
	private WechatTemplateDao wechatTemplateDao;
	
	public WechatTemplateService()
	{
	}
	
	public WechatTemplate save(WechatTemplate template)
	{
		if (wechatTemplateDao.exist(template, "wechatId", "code"))
			throw new WechatTemplateException("编号【" + template.getCode() + "】已经存在");
		
		if (template.getId() == null)
		{
			wechatTemplateDao.save(template);
		}
		else
		{
			wechatTemplateDao.update(template);
		}
		WechatTemplate w = wechatTemplateDao.get(template.getId());
		return w;
	}
	
	public WechatTemplate get(Integer id)
	{
		WechatTemplate template = wechatTemplateDao.get(id);
		return template;
	}
	
	public List<WechatTemplate> list(WechatTemplate template)
	{
		List<WechatTemplate> templateList = wechatTemplateDao.list(template);
		return templateList;
	}
	
	public Page<WechatTemplate> list(WechatTemplate wechat, Page<WechatTemplate> page)
	{
		page = wechatTemplateDao.list(wechat, page);
		return page;
	}
	
}
